<?php
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
    exit('Access Denied');
}

$table_ad = C::t('#erling_gridad_combo#ad');
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// 处理表单提交
if(isset($_POST['adsubmit'])) {
    if(empty($_POST['expire_time'])) {
        cpmsg('请设置过期时间', '', 'error');
    }
    
    $expire_time = strtotime($_POST['expire_time']);
    if($expire_time < time()) {
        cpmsg('过期时间不能早于当前时间', '', 'error');
    }
    $data = array(
        'title' => daddslashes($_POST['title']),
        'url' => daddslashes($_POST['url']),
        'bgcolor' => daddslashes($_POST['bgcolor']),
        'color' => daddslashes($_POST['color']),
        'bold' => intval($_POST['bold']),
        'underline' => intval($_POST['underline']),
        'italic' => intval($_POST['italic']),
        'position' => intval($_POST['position']),
        'type' => daddslashes($_POST['type']),
        'sort' => intval($_POST['sort']),
        'status' => intval($_POST['status']),
        'expire_time' => $_POST['expire_time'] ? strtotime($_POST['expire_time']) : 0,
        'rent_credit' => intval($_POST['rent_credit'])
    );
    
    if($id) {
        $table_ad->update($id, $data);
        cpmsg('广告更新成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_combo&pmod=admin_ad_list', 'succeed');
    } else {
        $data['uid'] = $_G['uid'];
        $table_ad->insert($data);
        cpmsg('广告添加成功', 'action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_combo&pmod=admin_ad_list', 'succeed');
    }
}

// 获取广告数据
$ad = $id ? $table_ad->fetch($id) : array();

echo '<div class="container">';
echo '<form method="post" action="'.ADMINSCRIPT.'?action=plugins&operation=config&do='.$pluginid.'&identifier=erling_gridad_combo&pmod=admin_ad_edit'.($id ? '&id='.$id : '').'">';
echo '<input type="hidden" name="formhash" value="'.FORMHASH.'" />';

echo '<div class="form-header">';
echo '<h2>'.($id ? '编辑广告' : '添加新广告').'</h2>';
echo '</div>';

echo '<div class="form-group">';
echo '<label>用户id</label>';
echo '<input type="text" name="uid" value="'.dhtmlspecialchars($ad['uid']).'" class="form-input">';
echo '</div>';

echo '<div class="form-group">';
echo '<label>广告标题</label>';
echo '<input type="text" name="title" value="'.dhtmlspecialchars($ad['title']).'" class="form-input">';
echo '</div>';

echo '<div class="form-group">';
echo '<label>链接地址</label>';
echo '<input type="text" name="url" value="'.dhtmlspecialchars($ad['url']).'" class="form-input">';
echo '</div>';

echo '<div class="form-group">';
echo '<label>背景颜色</label>';
echo '<input type="text" name="bgcolor" value="'.dhtmlspecialchars($ad['bgcolor']).'" class="form-input color-input">';
echo '<input type="color" value="'.($ad['bgcolor'] ?: '#FFFFFF').'" onchange="this.previousElementSibling.value=this.value" class="color-picker">';
echo '</div>';

echo '<div class="form-group">';
echo '<label>文字颜色</label>';
echo '<input type="text" name="color" value="'.dhtmlspecialchars($ad['color']).'" class="form-input color-input">';
echo '<input type="color" value="'.($ad['color'] ?: '#000000').'" onchange="this.previousElementSibling.value=this.value" class="color-picker">';
echo '</div>';

echo '<div class="form-group">';
echo '<label>文字样式</label>';
echo '<div style="display: flex; gap: 15px;">';
echo '<label><input type="checkbox" name="bold" value="1"'.($ad['bold']?' checked':'').'> 加粗</label>';
echo '<label><input type="checkbox" name="underline" value="1"'.($ad['underline']?' checked':'').'> 下划线</label>';
echo '<label><input type="checkbox" name="italic" value="1"'.($ad['italic']?' checked':'').'> 斜体</label>';
echo '</div>';
echo '</div>';

echo '<div class="form-group">';
echo '<label>广告位置</label>';
echo '<select name="position" class="form-select">';
echo '<option value="2"'.($ad['position']==2?' selected':'').'>首页头部</option>';
echo '<option value="3"'.($ad['position']==3?' selected':'').'>首页中部</option>';
echo '<option value="4"'.($ad['position']==4?' selected':'').'>首页底部</option>';
echo '<option value="5"'.($ad['position']==5?' selected':'').'>帖子页右侧</option>';
echo '</select>';
echo '</div>';

echo '<div class="form-group">';
echo '<label>广告类型</label>';
echo '<select name="type" class="form-select">';
echo '<option value="grid"'.($ad['type']=='grid'?' selected':'').'>格子广告</option>';
echo '<option value="banner"'.($ad['type']=='banner'?' selected':'').'>横幅广告</option>';
echo '</select>';
echo '</div>';

echo '<div class="form-group">';
echo '<label>排序值</label>';
echo '<input type="text" name="sort" value="'.($id ? intval($ad['sort']) : 1).'" class="form-input">';
echo '</div>';

echo '<div class="form-group">';
echo '<label>状态</label>';
echo '<select name="status" class="form-select">';
echo '<option value="1"'.($ad['status']==1?' selected':'').'>正常</option>';
echo '<option value="2"'.($ad['status']==2?' selected':'').'>暂停</option>';
echo '<option value="3"'.($ad['status']==3?' selected':'').'>未审核</option>';
echo '</select>';
echo '</div>';

echo '<div class="form-group">';
echo '<label>过期时间</label>';
echo '<input type="datetime-local" name="expire_time" value="'.($ad['expire_time'] ? date('Y-m-d\TH:i', $ad['expire_time']) : '').'" class="form-input">';
echo '</div>';

echo '<div class="form-group">';
echo '<label>租金金币</label>';
echo '<input type="text" name="rent_credit" value="'.intval($ad['rent_credit']).'" class="form-input">';
echo '</div>';

echo '<div class="form-footer">';
echo '<button type="submit" name="adsubmit" class="form-submit">提交</button>';
echo '</div>';

echo '</form>';
echo '</div>';

echo '<style>
.form-header { margin-bottom: 20px; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: bold; }
.form-input, .form-select { width: 100%; padding: 8px; border: 1px solid #DDD; border-radius: 4px; }
.color-input { width: 100px; display: inline-block; }
.color-picker { vertical-align: middle; margin-left: 10px; height: 30px; width: 30px; padding: revert;}
.form-submit { background: #2B73B7; color: white; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; }
.form-submit:hover { background: #1E5A8F; }
</style>';